<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_right">
          <div>
            <el-button
              v-if="isAuth('module:business:client:delete')"
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_select">
        <div class="input">
          <el-select
            v-model="dataForm.organization_id"
            :placeholder="i18n('common.please_select') + i18n('organization.title')"
            clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in organizationList" :label="v.title" :key="k" :value="v.id">
            </el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select
            v-model="dataForm.level"
            :placeholder="i18n('common.please_select') + i18n('client.level')" clearable>
            <el-option v-for="(v, k) in levelList" :label="v.text" :key="k" :value="v.value"></el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select
            v-model="dataForm.demand__demand_type"
            :placeholder="i18n('common.please_select') + i18n('demand.demand_type')" clearable>
            <el-option v-for="(v, k) in demandTypeList" :label="v.text" :key="k" :value="v.value"></el-option>
          </el-select>
        </div>
        <div class="input">
          <el-cascader v-model="dataForm.industry_id" :placeholder="$t('common.please_select')+$t('example.industry_id')" :options="industryList" :props="levelProps" clearable></el-cascader>
        </div>
        <div class="input">
          <el-select
            v-model="dataForm.integrity_score"
            :placeholder="i18n('common.please_select') + i18n('client.integrity_score')" clearable>
            <el-option v-for="(v, k) in integrityScoreList" :label="v.text" :key="k" :value="v.value"></el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select v-model="dataForm.status" :placeholder="$t('common.please_select') + $t('common.status')" clearable>
            <el-option :label="$t('common.all')" value=""></el-option>
            <el-option :label="$t('common.show')" value="1"></el-option>
            <el-option :label="$t('common.hidden')" value="2"></el-option>
          </el-select>
        </div>
        <div class="date">
          <el-date-picker v-model="dataForm.create_time" type="daterange" :range-separator="$t('common.to')" :start-placeholder="$t('common.start_time')" :end-placeholder="$t('common.end_time')" clearable>
          </el-date-picker>
        </div>
        <div class="input">
          <el-button icon="Search" @click="getDataList(true)">
            {{ i18n('common.search') }}
          </el-button>
        </div>
      </div>

      <div class="admin_table_main color">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column :label="i18n('organization.title')" width="140">
            <template v-slot="scope">
              <span v-if="scope.row.organization">{{ scope.row.organization.title }}</span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('client.info')" min-width="240">
            <template v-slot="scope">
              <dl class="table_dl">
                <dd class="table_dl_dd_all_30">
                  <el-link v-if="isAuth('module:business:client:list:view')" @click="router.push({ name: 'module_business_client_list_view', query: { id: scope.row.id } })" :underline="false">
                    {{ scope.row.name }}
                    <el-icon class="mr5"><ArrowRight /></el-icon>
                    <el-icon class="mr10" color="#67C23A" v-if="scope.row.access_authority"><SuccessFilled /></el-icon>
                  </el-link>
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ scope.row.level.value }}级 | {{ scope.row.industry_info }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('client.new_demand')" width="150">
            <template v-slot="scope">
              <dl class="table_dl" v-if="scope.row.latest_demand">
                <dd class="table_dl_dd_all_30">
                  {{ scope.row.latest_demand.demand_time }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ scope.row.latest_demand.demand_type.text }}
                </dd>
              </dl>
              <span v-else>
                {{ i18n('common.empty') }}
              </span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('client.new_record')" width="160">
            <template v-slot="scope">
              <dl class="table_dl" v-if="scope.row.latest_trace">
                <dd class="table_dl_dd_all_30">
                  {{ scope.row.latest_trace.create_time_text }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ scope.row.latest_trace.type.text }}
                </dd>
              </dl>
              <span v-else>
                {{ i18n('common.empty') }}
              </span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('client.integrity_score')" width="100">
            <template v-slot="scope">
              <span :class="scope.row.integrity_score.value < 30 ? 'red' : (scope.row.integrity_score.value < 60 ? 'orange' : (scope.row.integrity_score.value < 90 ? 'blue' : 'green1'))">
                {{ scope.row.integrity_score.text }} ({{scope.row.integrity_score.value}})
              </span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('contact.salesperson')" width="150">
            <template v-slot="scope">
              <dl class="table_dl" v-if="scope.row.user">
                <dt>
                  <el-avatar :size="45" :src="scope.row.user.avatar" />
                </dt>
                <dd class="table_dl_dd_all_30">
                  {{ scope.row.user.nickname }}
                </dd>
                <dd class="table_dl_dd_all_16_gray" v-if="scope.row.user.detail">
                  {{ scope.row.user.detail.position }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column :label="$t('common.status')" width="80">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.status.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.enable')"
                :inactive-text="$t('common.disable')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'status')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column prop="create_time_text" :label="i18n('common.create_time')" width="100">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="200">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:business:client:delete')"
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>

              <el-dropdown class="more">
                <el-button icon="More">
                  {{ i18n('common.more') }}
                </el-button>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item v-if="isAuth('module:business:client:list:view')" @click="router.push({ name: 'module_business_client_list_view', query: { id: scope.row.id } })">
                      {{ i18n('client.view') }}
                    </el-dropdown-item>

                    <el-dropdown-item v-if="isAuth('module:business:contact:list:list')" @click="router.push({ name: 'module_business_contact_list_list', query: { client_id: scope.row.id } })">查看联系人</el-dropdown-item>
                    <el-dropdown-item v-if="isAuth('module:business:demand:list:list')" @click="router.push({ name: 'module_business_demand_list_list', query: { client_id: scope.row.id } })">查看需求</el-dropdown-item>
                    <el-dropdown-item v-if="isAuth('module:business:client:trace:list')" @click="router.push({ name: 'module_business_client_trace_list', query: { client_id: scope.row.id } })">
                      查看跟踪记录
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="client-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/client')

  const levelProps = ref({
    label: 'title',
    value: 'id',
    emitPath: false,
    checkStrictly: true
  })

  const organizationList = ref([])
  const levelList = ref([])
  const industryList = ref([])
  const demandTypeList = ref([])
  const integrityScoreList = ref([])

  const dataForm = reactive([
    'organization_id',
    'level',
    'industry_id',
    'integrity_score',
    'demand__demand_type',
    'status',
    'create_time',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const loadOrganizationList = () => {
    $http({
      url: $http.adornUrl('/organization/select'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        organizationList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadLevelList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'client_level',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        levelList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadIndustryList = () => {
    $http({
      url: $http.adornUrl('/industry/select'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        industryList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadDemandTypeList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'demand_type',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        demandTypeList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadIntegrityScoreList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'complete_level',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        integrityScoreList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  // 更改客户所属人
  const changeHandle = (id) => {
    $http({
      url: $http.adornUrl(`${model.value}/publicity`),
      method: 'post',
      data: {
        id: id
      },
    }).then(({ data }) => {
      if (data && data.status === 200) {
        $message({
          message: i18n('common.handle_success'),
          type: 'success',
          duration: 1500,
          onClose: () => {
            getDataList()
          },
        })
      } else {
        $message.error(data.message)
      }
    })
  }


  onMounted(() => {
    dataForm.type = 1

    getDataList(model)

    loadOrganizationList()
    loadLevelList()
    loadIndustryList()
    loadDemandTypeList()
    loadIntegrityScoreList()
  })
</script>

<style scoped lang="scss">
  .el-dropdown-menu {
    // overflow: visible !important;
  }
</style>
